package com.cyh.CharOccupyTwoByte;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author CYH
 * @date 2018/3/7
 *
 * 通过程序证明JAVA的char内部编码为UTF-16，而与Charset.defaultCharset()无关。
 */
public class CharCodeTest {

    /**
     * 将byte转换为对应的二进制字符串
     * @param src 要转换成二进制字符串的byte值
     * @return
     */
    public static String byteToBinary(byte src) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            result.append(src % 2 == 0 ? '0' : '1');
            src = (byte) (src >>> 1);
        }
        return result.reverse().toString();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "I AM 中国人";
        System.out.println(Charset.defaultCharset());
        byte[] utf16 = str.getBytes(StandardCharsets.UTF_16);
        System.out.println(Arrays.toString(utf16));
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
            byte high = (byte) (str.charAt(i) >>> 8);
            byte low = (byte) str.charAt(i);
            System.out.println(byteToBinary(high) + byteToBinary(low));
            System.out.println(byteToBinary(utf16[2 + 2 * i]) + byteToBinary(utf16[2 + 2 * i + 1]));
            System.out.println((byteToBinary(high) + byteToBinary(low))
                    .equals(byteToBinary(utf16[2 + 2 * i]) + byteToBinary(utf16[2 + 2 * i + 1])));
        }
    }

}
